﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using LoghogInterface.Commands;
using LoghogInterface.Modules;
using LoghogInterface.Models;

namespace LoghogInterface.ViewModels
{
    /// <summary>
    /// Модель представления окна входа в программу
    /// </summary>
    class ViewModelWindowInput : ViewModelBase
    {
        public delegate void InputEventHandler(int idUser, int idJournal);
        public event InputEventHandler OnInput;

        private ModelInput _modelInput = new ModelInput();
        private JournalSQLHelper _journalSqlHelper;
        private UserSQLHelper _userSqlHelper;

        public string Login 
        {
            get
            {
                return _modelInput.Login;
            }
            set
            {
                _modelInput.Login = value;
                OnPropertyChanged("Login");
            }
        }

        public string Password
        {
            get
            {
                return _modelInput.Password;
            }
            set
            {
                _modelInput.Password = value;
                OnPropertyChanged("Password");
            }
        }
        public ObservableCollection<string> Journals { get; set; }
        public string SelectedJournal { get; set; }

        public ViewModelWindowInput()
        {
            _journalSqlHelper = new JournalSQLHelper();
            _userSqlHelper = new UserSQLHelper();
            Journals = _journalSqlHelper.GetJournalsName();
        }

        #region Commands
        private DelegateCommand _input;
        public ICommand Input
        {
            get
            {
                if (_input == null)
                {
                    _input = new DelegateCommand(this.InputUser, this.CanInputUser);
                }
                return _input;
            }       
        }

        private void InputUser()
        {
            if (CheckInputData())
            {
                string hashPassword = this.GetHashPassword(Password);
                int idJournal = _journalSqlHelper.GetIdByName(SelectedJournal);
                int idUser = _userSqlHelper.GetID(Login, hashPassword);
                CallOnInput(idUser, idJournal);
                /*MainWindow mainWindow = new MainWindow(idUser, idJournal);
                mainWindow.ShowDialog();*/
            }
            else
            {
                MessageBox.Show("Данные указаны неверно.");
            }
        }

        public void CallOnInput(int idUser, int idJournal)
        {
            if (OnInput != null)
            {
                OnInput(idUser, idJournal);
            }
        }

        private bool CanInputUser()
        {
            if (Login == String.Empty ||
                Password == String.Empty ||
                Login == null ||
                Password == null ||
                SelectedJournal == null||
                !CheckInputData())
                return false;
            else
                return true;
        }

        private string GetHashPassword(string password)
        {
            HashingAlgorithms hashing = new HashingAlgorithms();
            return hashing.MD5(password);
        }

        private bool CheckInputData()
        {
            string hashPassword = this.GetHashPassword(Password);
            if (!_userSqlHelper.UsernameExist(Login) || !_userSqlHelper.PasswordExist(hashPassword))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion
    }
}
